package Controller.book;

import java.io.IOException;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Controller.BaseHttpServlet;
import Entity.Book;
import Entity.BookInvoice;
import Entity.Invoice;
import Entity.User;
import Services.WebService;


public class HomeServlet extends BaseHttpServlet {    
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) throws ServletException, IOException {
    	if(action==null){
    		this.showBook(request, response, webServices, action);
    	} else if(action.equals("purchase")){
    		this.purchaseBook(request, response, webServices, action);
    	}
    }
    
    private void showBook(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) {
    	Book book = webServices.getEntityManager().getBook(Integer.valueOf(request.getParameter("id")));
    	book.setNoOfView(book.getNoOfView()+1);
    	
    	webServices.getEntityManager().updateBook(book);
    	
    	int lp_earn = (int)Math.round(book.getPrice()*0.05);
    	
    	request.setAttribute("book", book);
    	request.setAttribute("lp_earn", lp_earn);
    	
    	this.forward(request, response, webServices, action, "/books/book.idv.jsp");
    }
    
    private void purchaseBook(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) {
    	String bookID = request.getParameter("id");
    	Book book = webServices.getEntityManager().getBook(Integer.valueOf(bookID));
    	String useLP = request.getParameter("lp_use");
    	String qty = request.getParameter("qty");
    	
    	String purchaseStatus = "";
    	
    	if(book.getQuantity() < Integer.valueOf(qty)){
    		purchaseStatus = "INVALID_QTY";
    	} 
    	else if(WebService.isInteger(useLP) && Integer.valueOf(useLP) < 0){
    		purchaseStatus = "INVALID_LP";
    	} 
    	else if(webServices.getCurrentUser() != null && Integer.valueOf(useLP) > webServices.getCurrentUser().getLoyaltyPoint()){
    		purchaseStatus = "INSUFF_LP";
    	} 
    	else {
    		if(Integer.valueOf(useLP) > book.getPrice()*Integer.valueOf(qty)){
    			useLP = Integer.toString(Math.round(book.getPrice()*Integer.valueOf(qty)));
    		}
    		
    		book.setQuantity(book.getQuantity()-Integer.valueOf(qty));
    		book.setNoOfPurchase(book.getNoOfPurchase()+Integer.valueOf(qty));
    		webServices.getEntityManager().updateBook(book);

    		Invoice invoice = new Invoice();
    		
    		if(webServices.getCurrentUser() != null){
    			invoice.setUser(webServices.getCurrentUser());
    		}
    		else {
    			User user = new User();
    			user.setId(1);
    			
    			invoice.setUser(user);
    		}
    		
			invoice.setDate(new Date(new java.util.Date().getTime()));
			invoice.setRefund(false);
			invoice.setIsRefunded(false);
			invoice.setAttn(request.getParameter("name_prefix") + request.getParameter("name"));
			invoice.setAddress(request.getParameter("address"));
			invoice.setRemarks(request.getParameter("remarks"));
			
			if(invoice.getUser().getId() != 1){
				User user = webServices.getCurrentUser();
				user.setLoyaltyPoint(user.getLoyaltyPoint() + Integer.valueOf(request.getParameter("lp_earn"))*Integer.valueOf(qty));
				
				if(Integer.valueOf(useLP) > 0){
					request.setAttribute("lp_use", useLP);
					
					user.setLoyaltyPoint(user.getLoyaltyPoint()-Integer.valueOf(useLP));
					invoice.setLoyaltyPointUsed(true);
				}
				else {
					invoice.setLoyaltyPointUsed(false);
				}
				
				webServices.getEntityManager().updateUser(user);
			}
			
			Invoice updatedInvoice = webServices.getEntityManager().newInvoice(invoice);
			request.setAttribute("invoice", updatedInvoice);
			
			BookInvoice bookInvoice= new BookInvoice();
			bookInvoice.setBook(book);
			bookInvoice.setInvoiceID(updatedInvoice.getId());
			bookInvoice.setQuantity(Integer.valueOf(qty));
			
			BookInvoice updatedBookInvoice = webServices.getEntityManager().newBookInvoice(bookInvoice);

			purchaseStatus = "DONE";
    	}
    	
    	request.setAttribute("status", purchaseStatus);
    	request.setAttribute("book", book);
    	this.forward(request, response, webServices, action, "/books/book.purchase.jsp");
    }
}